package cn.edkuir.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.edkuir.product.entity.AttrAttrgroupRelationEntity;
import cn.edkuir.product.entity.AttrEntity;
import cn.edkuir.product.service.AttrAttrgroupRelationService;
import cn.edkuir.product.service.AttrService;
import cn.edkuir.product.service.CategoryService;
import cn.edkuir.product.vo.AttrGroupRelationVo;
import cn.edkuir.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.edkuir.product.entity.AttrGroupEntity;
import cn.edkuir.product.service.AttrGroupService;
import cn.edkuir.common.utils.PageUtils;
import cn.edkuir.common.utils.R;



/**
 * 属性分组
 *
 * @author xinzi
 * @email 1228604898@qq.com
 * @date 2021-04-14 20:54:28
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//根据三级分类id查询三级分类层级路径
		Long catelogId = attrGroup.getCatelogId();
		attrGroup.setCatelogPath(categoryService.findCatelogPath(catelogId));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    //获取指定分组关联的所有属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", attrEntities);
    }

    //删除属性与分组的关联关系
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVoList) {
        attrService.deleteRelation(attrGroupRelationVoList);

        return R.ok();
    }

    //获取属性分组没有关联的其他属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrService.getNoRelationAttr(attrgroupId, params);

        return R.ok().put("page", pageUtils);
    }

    //添加属性与分组关联关系
//    @PostMapping("/attr/relation")
//    public R addRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVoList) {
//        attrAttrgroupRelationService.saveBatch(attrGroupRelationVoList);
//
//        return R.ok();
//    }
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities) {
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntities);

        return R.ok();
    }

    //获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        //1.查出当前分类下的所有属性分组
        //2.查出每个分组的所有属性
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data", data);
    }

}
